/*设置日期框*/
$(function () {
    laydate.render({
        elem:'[name=startDate]'
    });
    laydate.render({
        elem:'[name=endDate]'
    });
    initGrant();/*初始化授权模态框*/
    intiAddUserInfo();//添加用户
    initUpdateUserInfo();//初始化修改模态框
    templateRoleList();
});

/*格式化性别*/
function formatGender(gender) {
    return gender==1?'男':'女';
}
/*格式化状态*/
function formatState(state) {
    return state==1?'启用':'禁用';
}
function formatAge(birthday)
{
    var   r   =   birthday.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
    if(r==null)return   false;
    var   d=   new   Date(r[1],   r[3]-1,   r[4]);
    if   (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4])
    {
        var   Y   =   new   Date().getFullYear();
        return((Y-r[1]));
    }
    return("输入的日期格式错误！");
}

/*格式化操作-删除*/
function formatOption(){
    var del = "<a href='javascript:void(0);' class='glyphicon glyphicon-trash del'></a>";
    var edit = "<a href='javascript:void(0);' class='update'><i class='glyphicon glyphicon-edit'></i></a>";
    return del+"&nbsp;&nbsp;&nbsp;&nbsp;"+edit;
}
/*查询角色*/
function templateRoleList() {
    $.getJSON(ctx+"/sys/role/searchRole",{},function (data) {
        $(data).each(function () {
            var $option = "<option value='"+this.roleId+"'>"+this.roleLabel+"</option>";
            $($option).appendTo($(".modal-body").find("form [name=roleId]"));
            // $(".modal-body").find("form [name=roleId]").append($option);
        })
    })
}

/*删除和修改单击事件*/
window.optEvents={
    "click .del":function (e,value,row) {
        delUserInfo(row.userId,row.userName);
    },"click .update":function (e,value,row) {
        updateUserInfo(row);
    }
}

/*删除用户方法*/
function delUserInfo(userId,name){
    swal({
        title:"友情提示!",
        text:"确定要删除"+name+"吗",
        type:"warning",
        showCancelButton:true,
        confirmButtonText:"是的",
        confirmButtonColor:"green",
        cancelButtonText:"取消",
        cancelButtonColor:"#c00",
        allowOutsideClick:false
    }).then(function () {
        $.post(ctx+"/sys/user/deleteUser",{"userId":userId},function (data) {
            swal("信息提示",data,"success");
            $("#userTable").bootstrapTable("refresh");
        });
    });
}

/*修改用户信息模态框*/
function updateUserInfo(row) {
    $("#updateUserInfo form #createTime").val(row.createTime);
    $("#updateUserInfo #updateUserInfo_form").setForm(row);
    $("#updateUserInfo").dialog("open");
}

/*多条件查询用户*/
function search() {
    var param = {};
    param.userName = $("#condition [name=userName]").val();
    param.loginName = $("#condition [name=loginName]").val();
    param.mobileNo = $("#condition [name=mobileNo]").val();
    $("#userTable").bootstrapTable("refresh", {query: param});
}

/*初始化授权模态框*/
function initGrant() {
    $("#grant").dialog({
         title:'用户授权',
        btnValue:'授权',
        btnBind:function () {
            var userId = $("#userTable").bootstrapTable("getSelections");
            var roleId = $("#roleTable").bootstrapTable("getSelections");
            if(userId.length>0 && roleId.length>0) {
                var params = "userId=" + userId[0].userId + "&roleId=" + roleId[0].roleId;
                $.post(ctx + "/sys/user/grant", params, function (msg) {
                    $("#grant").dialog("close");
                    $("#userTable").bootstrapTable("refresh");
                    toastr.info(msg);
                    $("#roleTable").bootstrapTable("uncheckAll");
                });
            }else{
                $("#grant").dialog("close");
            }
        }
    });
}

/*初始化修改方法*/
function initUpdateUserInfo() {
    $("#updateUserInfo").dialog({
        title:'修改用户信息',
        btnValue:'修改',
        btnBind:function () {
            var $form = $(".modal-body").find("#updateUserInfo_form");
            var params = $form.serialize();
            var loginName = $form.find("[name=loginName]").val();
            var userName = $form.find("[name=userName]").val();
            var roleId = $form.find("[name=roleId]").val();
            var mobileNo = $form.find("[name=mobileNo]").val();

            if(loginName==null || loginName=='' || userName==null || userName==''){
                swal("信息提示","信息提示不完整，登录名，用户名，密码和确认密码必填!","warning");
                return;
            }
            if(roleId == -1){
                swal("信息提示","请选择角色!","warning");
                return;
            }
            if(mobileNo.length!=11){
                swal("信息提示","电话应为11位!","warning");
                return;
            }
            var regex = /^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))[0-9]{8}$/;
            if(!(regex.test(mobileNo))){
                swal("信息提示","电话格式不正确!应为“1开头 + 8/5/3/4 + 后九位11位手机号”","warning");
                return;
            }
            if(params!=null && params.length>0){
                $.post(ctx+"/sys/user/updateUser",params,function (msg) {
                    $("#updateUserInfo").dialog("close");
                    $("#userTable").bootstrapTable("refresh");
                    toastr.info(msg);
                });
            }else{
                $("#addUserInfo").dialog("close");
            }
        }
    });
}




/*初始化添加用户模态框*/
function intiAddUserInfo() {
    $("#addUserInfo").dialog({
        title:'添加用户',
        btnValue:'添加',
        btnBind:function () {
            var $form = $(".modal-body").find("#addUserInfo_form");
            var params = $form.serialize();
            var loginName = $form.find("[name=loginName]").val();
            var userName = $form.find("[name=userName]").val();
            var password = $form.find("[name=password]").val();
            var repassword = $form.find("#repassword").val();
            var roleId = $form.find("[name=roleId]").val();
            var mobileNo = $form.find("[name=mobileNo]").val();

            if(loginName==null || loginName=='' || userName==null || userName=='' ||
                password==null || password==''  || repassword==null || repassword==''){
                swal("信息提示","信息提示不完整，登录名，用户名，密码和确认密码必填!","warning");
                return;
            }
            if(password!=repassword){
                swal("信息提示","密码和确认密码必须一致!","warning");
                return;
            }
            if(roleId == -1){
                swal("信息提示","请选择角色!","warning");
                return;
            }
            if(mobileNo.length!=11){
                swal("信息提示","电话应为11位!","warning");
                return;
            }
            var regex = /^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))[0-9]{8}$/;
            if(!(regex.test(mobileNo))){
                swal("信息提示","电话格式不正确!应为“1开头 + 8/5/3/4 + 后九位11位手机号”","warning");
                return;
            }
            if(params!=null && params.length>0){
                $.post(ctx+"/sys/user/addUserInfo",params,function (msg) {
                    $("#addUserInfo").dialog("close");
                    $("#userTable").bootstrapTable("refresh");
                    toastr.info(msg);
                });
            }else{
                $("#addUserInfo").dialog("close");
            }
        }
    });
}

/*授权方法*/
    function grant() {
        var rows = $("#userTable").bootstrapTable("getSelections");
        if(rows.length!=1){
            swal("请选择一项且只能一项待授权！");
            return;
        }

        var roleId = rows[0].roleId;
        var userId = rows[0].userId;
        $("#roleTable").bootstrapTable("checkBy",{field:"roleId",values:[roleId]});
        $("#grant").dialog("open");
    }

/*添加用户*/
function addUserInfo() {
        $("#addUserInfo").find("form")[0].reset();
        $("#addUserInfo").dialog("open");
    }















